package com.yanhui.api.qq;

import com.google.gson.Gson;
import com.yanhui.modal.qq.QQMuisc;
import com.yanhui.modal.qq.QQNumber;
import com.yanhui.modal.qq.music.QzoneMusic;
import com.yanhui.modal.qq.music.Song;
import com.yanhui.utils.http._cmsUtils;
import com.yanhui.utils.qq._qqUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description qq相关工具类
 * 未测试，如出现bug请联系QQ490647751
 * @Author yanhui
 * @Date 2018-11-12 10:01
 */
public class QQApi {

    /**日志*/
    public static Logger logger = LoggerFactory.getLogger(QQApi.class);

    public static void main(String[] args) {
//       String a =  searchQQs("494808400");
//       System.out.println(a);

//        String qq = "490647751";
//        List<QQMuisc> q = getQQMusic(qq);
//        System.out.println(q);
//
//        List<String> res = addQQs("1242835023;2164504429;2251310477;2404696326;1368379301;2669317801;","2281783193");
//        System.out.println(res);

    }

    private static String returnImg(String src) {
        return "<img src=\""+src+"\" />";
    }

    /**
     * qq强制添加好友
     * @param qqs QQs号
     * @param fuin 我的QQ号
     * @return
     * tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=490647751&fuin=2281783193&website=www.oicqzone.com
     */
    public static List<String> addQQs(String qqs, String fuin){
        List<String> list = new ArrayList<String>();
        String[] qqs_ = qqs.split(";");
        for(String uin : qqs_){
            list.add("tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all" +
                    "&uin="+uin+"&fuin="+fuin+"&website=www.oicqzone.com");
        }
        return list;
    }

    /**
     * 获取qq空间背景音乐
     * @param qq QQ号
     * @return
     * xml返回：http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?uin=490647751&TYPE=16&PAGE_START=1&PAGE_END=10000&SELECT_FLAG=1
     * json返回：http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?uin=490647751&json=1&PAGE_START=1&PAGE_END=10000&SELECT_FLAG=1
     */
    public static List<QQMuisc> getQQMusic(String qq){
        List<QQMuisc> list = new ArrayList<QQMuisc>();
        String url = "http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?uin="+qq+"&json=1";
        _cmsUtils cms = new _cmsUtils();
        String html = cms.getHtmlResourceByUrl(url,"gbk");
        html = html.substring(0,html.lastIndexOf(")")) ;
        String json = html.replace("jsonCallback(","");
        QzoneMusic jsonRootBean = new Gson().fromJson(json, QzoneMusic.class);
        ArrayList<Song> songs = jsonRootBean.getQqmusic().getPlaylist().getSong();
        for(Song s : songs){
            list.add(new QQMuisc(s.getXsong_name(), s.getXsinger_name(), s.getXsong_url()));
        }
        return list;
    }

    /**
     * 查找Q群
     * @param qqs QQ群号
     * @return
     */
    public static String searchQQs(String qqs){
        String url = "http://qun.qq.com/member.html#gid="+qqs;
        return url;
    }

    /**
     * 通过QQ号获取QQ头像
     * @param qq QQ号
     * @param type 头像大小类型 1：100 2：140 3：640
     * @return
     * http://q.qlogo.cn/headimg_dl?bs=qq&dst_uin=490647751&fid=blog&spec=100
     */
    public static String getQQImage(String qq, int type){
        String url = "";
        if(type == 1){
            url = "http://q2.qlogo.cn/headimg_dl" +
                    "?bs="+qq+"&dst_uin="+qq+"&dst_uin="+qq+"&;dst_uin="+qq+"" +
                    "&spec=100&url_enc=0&referer=bu_interface&term_type=PC";
        }else if(type == 2){
            url = "https://q4.qlogo.cn/g?b=qq&nk="+qq+"&s=140";
        }else if(type == 3){
            url = "http://q1.qlogo.cn/g?b=qq&nk="+qq+"&s=640";
        }
        return returnImg(url);
    }
    /**
     * 通过QQ号获取空间头像
     * @param qq QQ号
     * @return
     */
    public static String getQzoneImage(String qq){
        String url = "http://qlogo4.store.qq.com/qzone/"+qq+"/"+qq+"/100";
        return returnImg(url);
    }


    /**
     * Q群获取邮箱
     * @param html QQ群号网页
     * @return
     */
    public static List<QQNumber> getQQsByGroup(String html) throws Exception{
        List<QQNumber> list = new ArrayList<QQNumber>();
        html = html.replaceAll("\\s", "");
        String pat = "<tdclass=\"td-user-nick\"><a><iclass=\"(.+?)\"data-id=\"(.+?)\"></i></a><imgclass=\"\"src=\"(.+?)\"id=\"(.+?)\"><span>(.+?)</span></td><tdclass=\"td-card\"><spanclass=\"white\"></span></td><td>(.+?)</td><td>(.+?)</td><td>(.+?)</td>";
        Pattern pattern1 = Pattern.compile(pat);
        Matcher matcher1 = pattern1.matcher(html);
        while(matcher1.find()){
            String userName = matcher1.group(5);
            userName = new String(userName.getBytes("gb2312"),"gbk");
            QQNumber qq = null;
            if(userName.length()<100){
                qq = new QQNumber();
                qq.setUserName(userName);
                qq.setQq(matcher1.group(6));
                qq.setEmail(matcher1.group(6)+"@qq.com");
                qq.setSex(matcher1.group(7));
                qq.setAge(matcher1.group(8).replace("年", ""));
                list.add(qq);
                logger.info("添加{}成功！",userName);
            }else{
                logger.info("添加{}失败！",userName);
            }
        }
        return list;
    }



    /**
     * 贴吧获取邮箱
     * @param url 贴吧网址
     * @param lastPage 页数
     * @return
     */
    public static List<String> getQQsByTieba(String url, int lastPage){
        String result = "";

        _cmsUtils cms = new _cmsUtils();

        for(int i =1; i<=lastPage; i++){
            String tiebaUrl = url+"?pn="+i;
            result += _qqUtils.getQQEmail(tiebaUrl, cms);
        }

        String[] results = result.split(";");
        List<String> list = new ArrayList<String>();
        for(String email : results){
            list.add(email);
        }
        List<String> emailList = new ArrayList(new HashSet(list));

        return emailList;
    }

}
